<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="中断原因" prop="fileType">
        <el-select
          v-model="queryParams.fileType"
          placeholder="请选择"
          clearable
          @change="handleQuery"
        >
          <el-option label="人员中断" value="1"></el-option>
          <el-option label="瓦斯中断" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="中断开始时间">
        <el-date-picker
          v-model="start"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changestart"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="中断结束时间">
        <el-date-picker
          v-model="end"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeend"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['emergency:sysEmergencyWarning:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['emergency:sysEmergencyWarning:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['emergency:sysEmergencyWarning:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="sysEmergencyWarningList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="预警主键ID" align="center" prop="waringId" />-->
      <el-table-column label="中断原因" align="center" prop="fileType">
        <template slot-scope="scope">
          <span>{{ scope.row.fileType == "1" ? "人员中断" : "瓦斯中断" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="中断开始时间"
        align="center"
        prop="startTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="中断时长" align="center" prop="duration" />
      <el-table-column
        label="中断结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['emergency:sysEmergencyWarning:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['emergency:sysEmergencyWarning:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预警信息发布对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        inline
      >
        <el-form-item label="预警地点" prop="warningAddress">
          <el-input
            v-model="form.warningAddress"
            placeholder="请输入预警地点"
          />
        </el-form-item>
        <el-form-item label="预警开始时间" prop="warningStarttime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.warningStarttime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择预警开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预警发布人" prop="warningRelease">
          <el-input
            v-model="form.warningRelease"
            placeholder="请输入预警发布人"
          />
        </el-form-item>
        <el-form-item label="预警内容">
          <!-- <editor
            v-model="form.warningContent"
            :min-height="192"
            style="width: 535px"
          /> -->
          <el-input
            v-model="form.warningContent"
            placeholder="请输入预警内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSysEmergencyWarning,
  getSysEmergencyWarning,
  delSysEmergencyWarning,
  addSysEmergencyWarning,
  updateSysEmergencyWarning
} from "@/api/security/trarun";

export default {
  name: "SysEmergencyWarning",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预警信息发布表格数据
      sysEmergencyWarningList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warningAddress: null,

        startTime: null,

        endTime: null,

        warningContent: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      start: [],
      end: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changestart(e) {
      this.start = e;
      this.queryParams.params = {};
      if (null != this.start && "" != this.start) {
        this.queryParams.params["beginStartTime"] = this.getTimeNow(
          this.start[0]
        );
        this.queryParams.params["endStartTime"] = this.getTimeNow(
          this.start[1]
        );
      }
      this.handleQuery();
    },
    changeend(e) {
      this.end = e;
      this.queryParams.params = {};
      if (null != this.end && "" != this.end) {
        this.queryParams.params["beginEndTime"] = this.getTimeNow(this.end[0]);
        this.queryParams.params["endEndTime"] = this.getTimeNow(this.end[1]);
      }
      this.handleQuery();
    },
    getTimeNow(time) {
      const date = new Date(time); //   Wed Aug 26 2020 11:14:44 GMT+0800 (中国标准时间)
      const year = date.getFullYear(); // 2020
      let month = date.getMonth() + 1; // 8
      let day = date.getDate(); // 26
      let h = date.getHours(); // 11
      let m = date.getMinutes(); // 14
      let s = date.getSeconds(); // 44
      if (month < 10) {
        month = "0" + month;
      }
      if (day < 10) {
        day = "0" + day;
      }
      if (h < 10) {
        h = "0" + h;
      }
      if (m < 10) {
        m = "0" + m;
      }
      if (s < 10) {
        s = "0" + s;
      }
      return year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s; // 返回时间格式
    },
    /** 查询预警信息发布列表 */
    getList() {
      this.loading = true;
      listSysEmergencyWarning(this.queryParams).then(response => {
        this.sysEmergencyWarningList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        waringId: null,

        warningAddress: null,

        warningStarttime: null,

        warningRelease: null,

        warningContent: null,

        createBy: null,

        updateBy: null,

        createTime: null,

        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params = {};
      this.start = []
      this.end = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.waringId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预警信息发布";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const waringId = row.waringId || this.ids;
      getSysEmergencyWarning(waringId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预警信息发布";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.waringId != null) {
            updateSysEmergencyWarning(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysEmergencyWarning(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const waringIds = row.waringId || this.ids;
      this.$confirm("是否确认删除当前选中数据？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delSysEmergencyWarning(waringIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>
